<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        {include file="common/head" /}
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form action="" id="form" class="layui-form" method="post">
                    <input type="hidden" name="id" value="{$info.id|default=0}">
                  <div class="layui-form-item">
                      <label for="parent_id" class="layui-form-label">
                          <span class="x-red">*</span>上级
                      </label>
                      <div class="layui-input-inline">
                          <div class="layui-input-inline">
                              <select id="parent_id" name="parent_id" lay-filter="parent_id">
                                  <option value="0">作为一级菜单</option>
                                  {foreach $plist as $key=>$vo }
                                  {neq name="vo['id']" value="$id"}
                                  <option value="{$vo['id']}">{$vo.name}</option>

                                  {notempty name="vo['child']"}
                                  {foreach $vo['child'] as $k1=>$two}
                                  {neq name="two['type']" value="2"}
                                  <option value="{$two['id']}">&nbsp;&nbsp;├{$two.name}</option>
                                  {/neq}

                                  {notempty name="two['child']"}
                                  {foreach $two['child'] as $k1=>$three}
                                  {neq name="three['type']" value="2"}
                                  <option value="{$three['id']}">&nbsp;&nbsp;&nbsp;&nbsp;├{$three.name}</option>
                                  {/neq}
                                  {/foreach}
                                  {/notempty}

                                  {/foreach}
                                  {/notempty}
                                  {/neq}
                                  {/foreach}
                              </select>

                            {empty name='pid'}
                              <script>$("#parent_id option[value = '{$info.parent_id}']").attr("selected","selected");</script>
                            {else /}
                              <script>$("#parent_id option[value = '{$pid}']").attr("selected","selected");</script>
                            {/empty}

                          </div>
                      </div>
                      <div class="layui-form-mid layui-word-aux">

                      </div>
                  </div>

                    <div class="layui-form-item">
                      <label for="name" class="layui-form-label">
                          <span class="x-red">*</span>菜单名称
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" value="{$info.name}" id="name" name="name" required="" lay-verify="required" autocomplete="off" class="layui-input">
                      </div>
                      <div class="layui-form-mid layui-word-aux">

                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label for="app" class="layui-form-label">
                          <span class="x-red">*</span>应用模块
                      </label>
                      <div class="layui-input-inline">
                          <select id="app" name="app" lay-filter="app" lay-verify="zm">
                              <option value="">请选择模块</option>

                              {volist name="module_list" id="vo"}
                                  <option value="{$vo}">{$vo}</option>
                              {/volist}

                          </select>
                          <script>$("#app option[value = '{$info.app}']").attr("selected","selected");</script>
                      </div>
                      <div class="layui-form-mid layui-word-aux">

                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label for="controller" class="layui-form-label">
                          <span class="x-red">*</span>控制器
                      </label>
                      <div class="layui-input-inline">
                          <select id="controller" name="controller" lay-filter="controller" lay-verify="zm">
                              <option value="">请选择控制器</option>

                              {volist name="control" id="vo"}
                              <option value="{$vo}">{$vo}</option>
                              {/volist}
                          </select>
                          <script>$("#controller option[value = '{$info.controller}']").attr("selected","selected");</script>
                      </div>
                      <div class="layui-form-mid layui-word-aux">
                          <span class="x-red">*</span>
                      </div>
                  </div>

                    <div class="layui-form-item">
                        <label for="action" class="layui-form-label">
                            <span class="x-red">*</span>方法
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" value="{$info.action}" id="action" name="action" required="" lay-verify="zm" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span class="x-red">*</span>
                        </div>
                    </div>

                    {empty name="info['parent_id']"}
                    {empty name="pid"}
                    <div class="layui-form-item" id="m_wz">
                        <label class="layui-form-label"><span class="x-red"></span>菜单位置</label>
                        <div class="layui-input-block">
                            <input type="radio" name="position" value="0" {eq name="info['position']" value="0"}checked{/eq} lay-skin="primary" title="左边菜单" checked="">
                            <input type="radio" name="position" value="1" {eq name="info['position']" value="1"}checked{/eq} lay-skin="primary" title="头部菜单">
                        </div>
                    </div>
                    {/empty}
                    {/empty}

                    <div class="layui-form-item">
                        <label for="icon" class="layui-form-label">
                            <span class="x-red"></span>图标
                        </label>
                        <div class="layui-input-inline back">

                            <div class="icon iconfont" style="font-size: 32px" id="icon_html">
                                {$info.icon}
                            </div>
                            <input type="hidden" value="{$info.icon}" id="icon" name="icon" required="" lay-verify="" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux" style="padding: 0;padding: 0 !important">
                            <span class="x-red"></span>
                            <a class="layui-btn" id="showIcon">选择图标</a>
                        </div>
                    </div>

                  <div class="layui-form-item">
                      <label class="layui-form-label"><span class="x-red"></span>状态</label>
                      <div class="layui-input-block">
                        <input type="radio" name="status" value="1" {eq name="info['status']" value="1"}checked{/eq} lay-skin="primary" title="显示" checked="">
                        <input type="radio" name="status" value="0" {eq name="info['status']" value="0"}checked{/eq} lay-skin="primary" title="隐藏">
                      </div>
                  </div>

                    <div class="layui-form-item">
                        <label for="type" class="layui-form-label">
                            <span class="x-red">*</span>类型
                        </label>
                        <div class="layui-input-inline">
                            <div class="layui-input-inline">
                                <select id="type" name="type" lay-filter="province">
                                    <option value="1">有界面可访问菜单</option>
                                    <option value="2">无界面可访问菜单</option>
                                    <option value="0">只作为菜单</option>
                                </select>
                                <script>
                                    $("#type option[value = '{$info.type}']").attr("selected","selected");
                                </script>
                            </div>
                        </div>
                        <div class="layui-form-mid layui-word-aux">

                        </div>
                    </div>

                  <div class="layui-form-item">
                      <label for="remark" class="layui-form-label">
                          <span class="x-red"></span>备注
                      </label>
                      <div class="layui-input-inline">
                          <textarea id="remark" name="remark" placeholder="请输入内容" class="layui-textarea">{$info.remark}</textarea>
                      </div>
                  </div>
                  <div class="layui-form-item">
                      <label for="ok" class="layui-form-label">
                      </label>
                      <button id="ok" class="layui-btn" lay-filter="add" lay-submit="">
                          {empty name="info['id']"}
                          增  加
                          {else /}
                          修  改
                          {/empty}

                      </button>
                  </div>
              </form>
            </div>
        </div>

        <div id="idbox" style="display: none">

        </div>
        <script>layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //自定义验证规则
                form.verify({
                    zm: function(value) {
                        if(!new RegExp("^[a-zA-Z0-9_\s·]+$").test(value)){
                            return '只能是字母或数字，字母和下划线开头';
                        }

                        if(!isNaN(value.substr(0,1))){
                            return '只能是字母或数字，字母和下划线开头';
                        }
                    }
                });

                //监听提交
                form.on('submit(add)',
                function(data) {
                    $.ajax({
                        type: "POST",
                        url: "{:url()}",
                        data: data.field,
                        success: function (result) {
                            layer.alert(result.msg, {
                                icon: 6
                            },function() {
                                xadmin.close();
                                xadmin.father_reload();
                            });
                        },
                        error: function(data) {
                            layer.alert(data.msg, {
                                    icon: 2
                                },function() {
                                    xadmin.close();
                                });
                        }

                    });
                    return false;
                });

                form.on('select(parent_id)',function(data) {
                        if(data.value==0){
                            $("#m_wz").show();
                        }else{
                            $("#m_wz").hide();
                        }
                    });

                $("#idbox").load("/static/admin/icon.html");
                var html="";
                $("#showIcon").click(function () {
                    if(html==""){
                        html=$("#idbox").html();
                        $("#idbox").html("")
                    }

                    layer.open({
                        type: 1,
                        shadeClose:true,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['800px', '100%'], //宽高
                        content: html
                    });
                });

                form.on('select(app)',function(data) {
                    var v=data.value;
                    console.log(v);
                    if(v=="") return false;

                    $.ajax({
                        type: "POST",
                        url: "{:url('getControllers')}",
                        data: {module:v},
                        success: function (result) {
                            $("#controller option:not(:first)").remove();
                            $.each(result, function (index, item) {
                                $('#controller').append(new Option(item, item));// 下拉菜单里添加元素
                            });

                            form.render('select');
                        },
                        error: function(data) {

                        }

                    });
                });

            });
        </script>

    </body>

</html>
